/**
 * File: MySQLConnection.h
 * Created Time: 2024-12-30
 * Author: chugehehe (3321592850@qq.com)
 */



#ifndef MYSQL_CONNECTION_H
#define MYSQL_CONNECTION_H

// 数据库连接
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/metadata.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>

#include <memory>
#include <string>

using std::string;
using std::unique_ptr;

#define IP "127.0.0.1"
#define USER "root"
#define PASSWORD "1234"
#define DATABASE "test"

class MySQLConnection
{
public:
    MySQLConnection(const string &ip = IP, const string &user = USER, const string &password = PASSWORD, const string &database = DATABASE);
    ~MySQLConnection();

    void query(const string &query);// 执行查询
    void update(const string &query);// 执行更新
    void transaction(const string &query);// 执行事务

private:
    sql::mysql::MySQL_Driver *_driver;// 数据库驱动
    unique_ptr<sql::Connection> _conn;// 数据库连接
    unique_ptr<sql::Statement> _stmt;// 数据库语句
    unique_ptr<sql::ResultSet> _res;// 数据库结果集
    unique_ptr<sql::PreparedStatement> _pstmt;// 数据库预处理语句
};

#endif